package com.ctbri.vSearch.dao.neo4j;

import com.ctbri.vSearch.dao.Neo4j2Redis;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.ctbri.vSearch.bean.po.Link;
import com.ctbri.vSearch.bean.po.Node;
import com.ctbri.vSearch.frame.Neo4jPooledConnection;
import com.ctbri.vSearch.util.TimeUtil;
import redis.clients.jedis.Jedis;

@Repository
public class Neo4jQueryDao {
	public Neo4jQueryDao(){
		
	}
	@Resource
	Neo4jPooledConnection neo4jPooledConnection;

	//查询小图
	public  void firstQuery(String word,List<String> nodes ,List<String> links, ObjectMapper mapper, String[] category ,Jedis client){
		Connection con = neo4jPooledConnection.getCurrentConnecton();
		Neo4j2Redis n2r = new Neo4j2Redis();
		String queryCat = "match(n{content:'" + word
				+ "'})-[r]-(f) RETURN f as node, r as dis ORDER by r.weight DESC limit 30";
		PreparedStatement stmtCat = null; // 采用预编译，和关系数据库不一样的是,参数需要使用{1},{2},而不是?
		ResultSet rsCat = null;
		Node center = new Node();
		try {
			stmtCat = con.prepareStatement(queryCat);
			rsCat = stmtCat.executeQuery();
			if (rsCat.next()) {
				category[0] = rsCat.getString(1);
				center.setCategory(category[0]);
				center.setLabel(word);
				center.setValue((double) 1);
				center.setName(word);
				String centerStr = mapper.writeValueAsString(center);
				nodes.add(centerStr);
				n2r.createNeo4j2RedisByWord(word, client);
				n2r.putNode2Redis(word, word, category[0], word, (double) 1, client, true);
			}
			for(String node:nodes){
				System.out.print("nodes:"+node+" ");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != rsCat) {
				try {
					rsCat.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (null != stmtCat) {
				try {
					stmtCat.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			neo4jPooledConnection.releaseConnection(con);
		}
		return ;
	}
	
	public  void secondQuery(String word,List<String> nodes ,List<String> links, ObjectMapper mapper , Jedis client){
		Connection littleMap = neo4jPooledConnection.getCurrentConnecton();
		Neo4j2Redis n2r = new Neo4j2Redis();
		//第二次查询查小图
		String queryCont = "MATCH (n {content:'" + word.toLowerCase() + "'} )-[r1]->(f1) "
				+ "where f1.category = 'person' " + "RETURN r1 as dis, f1 as layernode "
				+ "ORDER BY r1.weight DESC limit 2 " + "UNION " + "MATCH (n{content:'" + word.toLowerCase()
				+ "'})-[r2]->(f2) " + "where f2.category = 'location' " + "RETURN r2 as dis, f2 as layernode "
				+ "ORDER BY r2.weight DESC limit 2 " + "UNION " + "MATCH (n{content:'" + word.toLowerCase()
				+ "'})-[r3]->(f3) " + "where f3.category = 'institution' " + "RETURN r3 as dis, f3 as layernode "
				+ "ORDER BY r3.weight DESC limit 2";
		PreparedStatement stmtCont = null; // 采用预编译，和关系数据库不一样的是,参数需要使用{1},{2},而不是?
		ResultSet rsCont = null;
		String category = "";
		try {
			stmtCont = littleMap.prepareStatement(queryCont);
			rsCont = stmtCont.executeQuery();
			
			while (rsCont.next()) {
				// 加入一条边
				JSONObject linkJson = JSONObject.parseObject(rsCont.getString(1));
				JSONObject nodeJson = JSONObject.parseObject(rsCont.getString(2));
				Link link = new Link();
				link.setName(nodeJson.getString("category"));
				link.setSource(linkJson.getString("from"));
				link.setTarget(linkJson.getString("to"));
				link.setWeight((double) 2);
				String linkStr = mapper.writeValueAsString(link);
				links.add(linkStr);
				n2r.putLink2Redis(word, nodeJson.getString("category"),
						linkJson.getString("from"), linkJson.getString("to"), (double) 2, client);
				// 加入一个点
				Node node = new Node();
				node.setCategory(nodeJson.getString("category"));
				node.setValue((double) 1);
				node.setName(nodeJson.getString("content"));
				String nodeStr = mapper.writeValueAsString(node);
				nodes.add(nodeStr);
				n2r.putNode2Redis(word,nodeJson.getString("content"),
						nodeJson.getString("category"),nodeJson.getString("content"),(double)1,client,false);
			}
			for(String node:nodes){
				System.out.println("nodes: "+node+" ");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != rsCont) {
				try {
					rsCont.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (null != stmtCont) {
				try {
					stmtCont.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			neo4jPooledConnection.releaseConnection(littleMap);
		}
		
	}
	
	
}
